package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.model.SupervisionRecords;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 督查管理-督查记录
 * Created by mz03 on 2017/3/22.
 * @author :zyp
 */
@Repository
public class SupervisionRecordsDao extends MyBatisDao<SupervisionRecords> {

    /*
    不根据任何条件查询全部
     */
    public List<SupervisionRecords> findByAll() {
        return getSqlSession().selectList(sqlId("findByAll"));
    }

    /**
     * 根据status查找
     */
    public List<SupervisionRecords> findByStatus(Integer status) {
        return getSqlSession().selectList(sqlId("findByStatus"), status);
    }

    /**
     * "不分页.去重"  可同时用于查询一人和所有
     * 用来显示督查轨迹地图上的养殖场
     */
    public List<SupervisionRecords> findByMonth(Map map) {
        return getSqlSession().selectList(sqlId("findByMonth"), map);
    }

    /**
     * 分页.不去重
     * 根据状态查询当月所有人的督查记录 用来显示督查轨迹地图上的养殖场
     */
    public Paging<SupervisionRecords> pagingFindByMonth(Integer offset, Integer limit, Map<String, Object> criteria) {
        if (criteria == null) {
            criteria = Maps.newHashMap();
        }

        Long total = (Long) this.sqlSession.selectOne(this.sqlId("countByMonth"), criteria);
        if (total.longValue() <= 0L) {
            return new Paging(Long.valueOf(0L), Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List datas = this.sqlSession.selectList(this.sqlId("pagingByMonth"), criteria);
            return new Paging(total, datas);
        }
    }


    /**
     * 根据养殖场Id查询存在这个养殖场上的督查轨迹,并且是当月的,所有的督查记录.     用来组SupervisionRecordsDTO
     */
    public List<SupervisionRecords> findByBreedFactoryId(Map map) {
        return getSqlSession().selectList(sqlId("findByBreedFactoryId"), map);
    }

    /**
     * 返回当前单据的编码的最大值 传入的Number是编号的前缀,用来判断前缀相等的最大的编号
     *
     * @param number 除去流水号的前缀
     * @return string
     */
    public String findCurrentMaxNumber(String number) {
        return getSqlSession().selectOne(sqlId("findCurrentMaxNumber"), number);
    }


    public List<String> findAllName() {

        return getSqlSession().selectList(sqlId("findAllName"));
    }


    public Paging<SupervisionRecords> pagingcommon(Integer offset, Integer limit, Map criteria) {
        Long total = (Long) this.sqlSession.selectOne(this.sqlId("countcommon"), criteria);
        if (total.longValue() <= 0L) {
            return new Paging();
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List datas = this.sqlSession.selectList(this.sqlId("pagingcommon"), criteria);
            return new Paging(total, datas);
        }
    }



    /**
     * 分页.不去重
     * 根据状态查询当月所有人的督查记录 用来显示督查轨迹地图上的养殖场
     */
    public Paging<SupervisionRecords> pagingByStatus(Integer offset, Integer limit, Map<String, Object> criteria) {
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }

        Long total = (Long)this.sqlSession.selectOne(this.sqlId("countByStatus"), criteria);
        if(total <= 0L) {
            return new Paging(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List datas = this.sqlSession.selectList(this.sqlId("pagingByStatus"), criteria);
            return new Paging(total, datas);
        }
    }

}

